package com.subway.babynote.syncservice;

import java.util.*;

import android.util.Log;
public class taskList {
	public static class tsPair
	{
		public static class TSPAIR
		{
			public tsPair ts;
			public TSPAIR(tsPair t)
			{
				this.ts = t;
			}
		}
		private int client_ts;
		private int server_ts;
		private boolean haswork;
		private boolean is_c2s;
		public tsPair(int c, int s)
		{
			this.client_ts = c;
			this.server_ts = s;
			this.haswork = false;
			this.is_c2s = true;
		}
		public tsPair(int c, int s, boolean c2s)
		{
			this.client_ts = c;
			this.server_ts = s;
			this.haswork = false;
			this.is_c2s = c2s;
		}
		public boolean set_client_ts(int c_ts)
		{
			try
			{
				synchronized(this)
				{
					this.client_ts = Math.max(this.client_ts, c_ts);
					this.checkwork();					
					return this.haswork;
				}
			}
			catch(Exception e)
			{
				Log.i("c2s", e.getMessage());
				return false;
			}
		}
		public boolean clear_client_ts(int c_ts)
		{
			try
			{
				synchronized(this)
				{
					this.client_ts = c_ts;
					this.checkwork();					
					return this.haswork;
				}
			}
			catch(Exception e)
			{
				Log.i("c2s", e.getMessage());
				return false;
			}
		}
		public boolean set_server_ts(int s_ts)
		{
			try
			{
				synchronized(this)
				{
					this.server_ts = Math.max(this.server_ts, s_ts);					
					this.checkwork();					
					return this.haswork;
				}
			}
			catch(Exception e)
			{
				Log.i("c2s", e.getMessage());
				return false;
			}
		}
		public int get_cts()
		{
			try
			{
				synchronized(this)
				{	
					return this.client_ts;
				}
			}
			catch(Exception e)
			{
				Log.i("c2s", e.getMessage());
				return 0;
			}
		}
		public int get_sts()
		{
			try
			{
				synchronized(this)
				{					
					return this.server_ts;
				}
			}
			catch(Exception e)
			{
				Log.i("c2s", e.getMessage());
				return 0;
			}
		}
		
		public boolean setTS(int c, int s, boolean c2s)
		{
			try
			{
				synchronized(this)
				{
					this.is_c2s = c2s;
					this.client_ts = c;
					this.server_ts = s;
					this.checkwork();		
					return this.haswork;
				}
			}
			catch(Exception e)
			{
				Log.i("c2s", e.getMessage());
				return false;
			}
		}
		private boolean checkwork()
		{
			if(is_c2s)
				this.c2s();
			else
				this.s2c();
			return this.haswork;
		}
		public boolean checkwork_available()
		{
			try
			{
				synchronized(this)
				{					
					return this.checkwork();
				}
			}
			catch(Exception e)
			{
				Log.i("c2s", e.getMessage());
				return false;
			}
		}
		
		private boolean c2s()
		{
			if(this.client_ts > this.server_ts)
				this.haswork = true;
			else
				this.haswork = false;
			return this.haswork;
		}
		
		private boolean s2c()
		{
			if(this.server_ts > this.client_ts)
				this.haswork = true;
			else
				this.haswork = false;
			return this.haswork;
		}
	}
	
	Map<Integer, tsPair> m = new TreeMap<Integer,tsPair>();
	
	private boolean Init()
	{
		/*
		for(int i = 1;i<=syncapi.operation_type.operation_length;i++)
		{
			m.put(i, new tsPair(0,0));
		}
		*/
		m.put(syncapi.operation_code.addFeedback, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.addFeedRecord, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.changeUserInfo, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.getFeeds, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.getFoodprop, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.getFoods, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.addh, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.addw, new tsPair(-1,-1,true));
		
		m.put(syncapi.operation_code.getLastAverage, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.getTip, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.getts, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.loginUser, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.regUser, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.regVisitor, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.getts2, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.getcontact, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.getloadinfo, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.get_h_default, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.get_h_ux, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.get_w_default, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.get_w_ux, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.get_food_default, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.get_food_ux, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.geth, new tsPair(-1,-1,false));
		m.put(syncapi.operation_code.getw, new tsPair(-1,-1,false));
		
		m.put(syncapi.operation_code.syncweibo, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.getweibo, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.logout, new tsPair(-1,-1,true));
		m.put(syncapi.operation_code.unbindweibo, new tsPair(-1,-1,true));
		return true;
	}
	public taskList()
	{
		this.Init();
	}
	
	public tsPair get_tspair(int id)
	{
		if(id <= syncapi.operation_type.operation_length)
			return this.m.get(new Integer(id));
		else
			return null;
	}
	
}
